/* 
 *基于Jquery 和 artTemplate 封装的列表控件
 *可实现分页和无分页列表，功能单一欢迎大家补充
 * Date: 2015-04-28
            datagrid = $("#eTable").datagrid({
                //pageNo: 1,
                //pageSize: 10,
                //queryParams: $("#search").serialize(),
                url: "/Layui/GetJson",
                pagination: "pagination",
                scriptHtml: "eTableHtml",
                table: "eTableRow",
                isPagination: true,
                //onLoadSuccess: function (data) {
                //    //alert(data);
                //}
            });
 */
(function ($) {
    function init(options, obj) {
        function getParam() {
            var param = "pageNo=" + opts.pageNo + "&pageSize=" + opts.pageSize;
            param = param + "&" + opts.queryParams;
            return param;
        }
        function queryForm() {
//            var cells = document.getElementById(opts.table).rows.item(0).cells.length;
//            if (opts.isPagination) {
//                document.getElementById(opts.pagination).innerHTML = "";
//            }
//            var trStr = "<tr><td colspan=" + cells + " style='text-align:center'>{0}</td></tr>";
//            obj.html(trStr.replace("{0}", "<img src='../images/load/loading.gif'/>数据正在加载中..."));
            var url = opts.url + "?ts=" + Math.random();
            //var url = opts.url;
            $.post(url, getParam(), function (result) {
            	//console.log(result.data.result);
                if (result.data.result == null || typeof (result.data.result) == "undefined") {
                    //obj.html(trStr.replace("{0}", "<img width='18' src='/Scripts/datagrid/images/smiley_027.png'/>没有查询到您想要的数据"));
                	obj.html(trStr.replace("{0}", "没有查询到您想要的数据"));
                	return;
                }
                data.list = result.data.result;
                data.total =  result.data.total;
//                console.log(data);
                var html = template(opts.scriptHtml, data);
                obj.html(html);
//                console.log(result.data.total);
//                console.log(opts.isPagination);
                total = result.data.total;
                if (total > 0 && opts.isPagination) {
//                    console.log(opts.pagination);
//                    console.log(opts.pageNo);
//                    console.log(opts.pageSize);
//                    console.log(total);
                    pageInitialize(opts.pagination, opts.pageNo, opts.pageSize, total);
                }
                callbackFun();
            },"json");
        }

        function pageInitialize(pageID, pageNo, pageSize, total) {
            laypage({
                cont: pageID, //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
                pages: Math.ceil(total / pageSize), //通过后台拿到的总页数
                curr: pageNo, //初始化当前页
                //skin: 'yahei', //加载内置皮肤，也可以直接赋值16进制颜色值，如:#c00
                groups: 5,
                first: false, //若不显示，设置false即可
                last: false, //若不显示，设置false即可
                prev: '<', //若不显示，设置false即可
                next: '>', //若不显示，设置false即可
                jump: function (e, first) { //触发分页后的回调
                    opts.pageNo = e.curr;
                    if (!first) { //一定要加此判断，否则初始时会无限刷新
                        queryForm();
                    }
                }
            });
        }

        function callbackFun() {
            if (opts.onLoadSuccess != null) {
                opts.onLoadSuccess();
            }
        }

        var defaults = {
            pageSize: 10,
            pageNo: 1,
            queryParams: "",
            pagination: "",
            scriptHtml: "",
            table: "",
            url: "",
            isPagination: false,
            onLoadSuccess: null
        }

        var opts = $.extend(defaults, options);
        var data = new Array();
        var total;
        queryForm();

        var method = {};
        return method.getpageNo = function () {
            return this.pageNo;
        },//当前页刷新
        method.onReload = function () {
            queryForm();
        },//重新加载
        method.onLoad = function () {
            opts.pageNo = 0;
            queryForm();
        },
        method.getData = function () {
            return data;
        },
        method.gettotal = function () {
            return total;
        },
        method
    }

    $.fn.datagrid = function (options) {
        return init(options, $(this));
    }
})(jQuery)